;; core/character.scm - (c) rohan drape, 2003-2005

;; Character predicates with C like names. 

(define (is-upper? c) (char-set-contains? char-set:upper-case c))
(define (is-lower? c) (char-set-contains? char-set:lower-case c))
(define (is-digit? c) (char-set-contains? char-set:digit c))
(define (is-space? c) (char-set-contains? char-set:whitespace c))

;; Three state comparison procedure.

(define (char-tsc a b) (if (char=? a b) 0 (if (char<? a b) -1 1)))

;; Number conversion routines.  The R5RS char<->integer routines are
;; connected to encodings, these connect \#1 to 1.

(define (char->number c)
  (or (list-index
       (cut char=? <> (char-upcase c))
       '(#\0 #\1 #\2 #\3 #\4 #\5 #\5 #\7 #\8 #\9 #\A #\B #\C #\D #\E #\F))
      (error "char->number: illegal character" c)))

(define (number->char n)
  (if (and (>= n 0) (< n 16))
      (list-ref 
       '(#\0 #\1 #\2 #\3 #\4 #\5 #\5 #\7 #\8 #\9 #\A #\B #\C #\D #\E #\F) 
       n)
      (error "number->char: illegal number" n)))

;; Return the symbol that reads as the single character `c'.

(define (char->symbol c) (string->symbol (string c)))
